package com.gewu.ordlelist.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gewu.ordlelist.R;
import com.gewu.ordlelist.base.BaseActivity;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.database.dbdao.ByCarDao;
import com.gewu.ordlelist.utils.jocUtils;

import java.util.List;
import java.util.Random;

public class moreActivity extends BaseActivity{

    private Toolbar toolbar;
    private android.widget.TextView text;
    private android.widget.TextView tabs;
    private androidx.recyclerview.widget.RecyclerView rec;
    private android.widget.ImageView image;
    private android.widget.Button addAction;

    @Override
    public int FindLayout() {
        return R.layout.activity_more;
    }

    @Override
    public Toolbar getToolBar() {
        return toolbar;
    }

    @Override
    public boolean isShowBackToolbar() {
        return true;
    }

    @Override
    public String setToolbarText() {
        return "菜品详情";
    }


    @Override
    public void initview() {
        toolbar = findViewById(R.id.toolbar);
        text = findViewById(R.id.text);
        tabs = findViewById(R.id.tabs);
        rec = findViewById(R.id.rec);
        image = findViewById(R.id.image);
        addAction = findViewById(R.id.addAction);
        toolbar.setTitle(setToolbarText());
        int index = getIntent().getIntExtra("index", 0);
        String name = getIntent().getStringExtra("name");
        Glide.with(this).load(jocUtils.getimgs()[index > 5 ? new Random().nextInt(6) : index]).into(image);


        addAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("TAG", "onClick: " + name);
                ByCarDao byCarDao = new ByCarDao(moreActivity.this);
                List<ByCar> byCars = byCarDao.querLike(name);
                if (!byCars.isEmpty()) {
                    AlertDialog.Builder builder = jocUtils.showDilog(moreActivity.this, "提示！", "已经添加！");
                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
                }else {
                    ByCar byCar = new ByCar();
                    byCar.setName(name);
                    byCar.setMoney(String.valueOf(new Random().nextInt(1000)));
                    byCar.setNum(1);
                    byCarDao.add(byCar);
                    AlertDialog.Builder builder = jocUtils.showDilog(moreActivity.this, "提示！", "成功添加！");
                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
                }
            }
        });

    }

}